#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @time     : 2018/2/22 13:56
# @Author   : BYF
# file      : base_page.py
# @version  :

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class BasePage(object):
    def __init__(self, driver, config):
        self.driver = driver
        self.config = config

    def open(self, url):
        self.driver.get(url)
        return self.driver

    def visit(self, url):
        """
        访问URL
        :param url:URL
        :return:
        """
        self.driver.get(url)

    def locator(self, locator, driver=None):
        """
        元素定位
        :param locator: 元素定位器
        :return:
        """
        if driver is None:
            driver = self.driver
        return driver.find_element(*locator)

    def input_text(self, locator, str):
        """
        输入框输入
        :param locator: 元素定位器
        :param str: 输入字符串
        :return:
        """
        element = self.locator(locator)
        if not element.get_attribute("readonly"):
            element.clear()
            element.send_keys(str)


    def click_element(self, locator):
        """
        元素点击
        :param locator:元素定位器
        :return:
        """
        element = self.locator(locator)
        if not element.get_attribute("readonly"):
            element.click()

    def check(self, list, name):
        """
        下拉选择
        :param list:下拉列表
        :param name:选择内容
        :return:
        """
        for option in list:
            if option.text == name:
                option.click()
                break

    def switch_to_iframe(self, iframe_loc):
        """
        进入iframe
        :param iframe_loc: iframe定位器
        :return:
        """
        WebDriverWait(self.driver, 30, 0.5).until(EC.presence_of_element_located(iframe_loc))
        # iframe = self.driver.find_element(*iframe_loc)
        iframe = self.locator(iframe_loc)
        self.driver.switch_to.frame(iframe)

    def quit(self):
        self.driver.quit()
